package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.CategoryMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Category;
import com.sky.result.PageResult;
import com.sky.service.common.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    //新增菜品分类
    @Override
    public void insert(Category category) {
        //分类状态默认为禁用状态0
        category.setStatus(StatusConstant.DISABLE);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());

        categoryMapper.insert(category);
    }

    //分类的分页查询
    @Override
    public PageResult CategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        //开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.CategoryPage(categoryPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除菜品分类
    public void daleteById(Long id) {
        //查询当前分类是否关联了菜品，如果关联了就抛出业务异常
        Integer count = dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        //查询当前分类是否关联了套餐，如果关联了就抛出业务异常
        count = setmealMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteById(id);

    }
    //启动禁用
    @Override
    public void startAndStop(int status, int id) {
        categoryMapper.startAndStop(status,id);
    }
    //修改菜品分类
    @Override
    public boolean updateCategory(Category category) {
        //设置更新人和更新时间
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        int row = categoryMapper.updateCategory(category);

        return row > 0;
    }
    //根据类型查询菜品分类
    @Override
    public List<Category> findByType(Integer type) {
        return categoryMapper.findByType(type);
    }

    //查询所有菜品分类
    @Override
    public List<Category> findCategory() {
        return categoryMapper.findCategory();
    }

    //根据菜品分类查询
    @Override
    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.list(type);

        return list;
    }

}
